package com.dream.service.recommender.web.processor.impl;

import com.alibaba.fastjson.JSONObject;
import com.dream.common.model.response.BaseResponse;
import com.dream.common.model.response.ErrorCode;
import com.dream.common.model.result.AppResultBuilder;
import com.dream.service.recommender.web.model.PPCShowStyle;
import com.dream.service.recommender.web.parse.Parser;
import com.dream.service.recommender.web.parse.impl.ParamParser;
import com.dream.service.recommender.web.processor.RequestProcessor;
import com.dream.service.recommender.api.RouteService;
import com.dream.service.recommender.domain.request.BaseRequest;
import com.dream.service.recommender.domain.request.FrontParam;
import com.dream.service.recommender.domain.response.RecommenderData;

import javax.servlet.http.HttpServletRequest;
import java.security.InvalidParameterException;

/**
 * Created by zhiji on 2016/3/24.
 */
public class OpenRequestProcessor implements RequestProcessor {

    private RouteService routeService;

    public RouteService getRouteService() {
        return routeService;
    }

    public void setRouteService(RouteService routeService) {
        this.routeService = routeService;
    }

    private PPCShowStyle ppcShowStyle = new PPCShowStyle();

    @Override
    public JSONObject processor(HttpServletRequest req) {

        JSONObject result = new JSONObject();
        if (routeService == null) {
            AppResultBuilder.buildProcessErrorResult(result, "");
            return result;
        }
        try {
            Parser parser = new ParamParser();
            FrontParam frontParam = null;
            try {
                frontParam = parser.parseForOpen(req);
            } catch (InvalidParameterException e) {
                AppResultBuilder.buildParamErrorResult(result, e.getMessage());
                return result;
            }
            BaseRequest<FrontParam> baseRequest = new BaseRequest<FrontParam>();
            baseRequest.setParam(frontParam);
            BaseResponse<RecommenderData> baseResponse = routeService.getRecommender(baseRequest);
            if (baseResponse.isSuccess() && baseResponse.getError_code() == ErrorCode.OK.getCode()) {
                RecommenderData recommenderData = baseResponse.getResult();
                if (recommenderData != null && recommenderData.getList() != null && !recommenderData.getList().isEmpty()) {
                    JSONObject subJSON = JSONObject.parseObject(JSONObject.toJSONString(recommenderData));
                    subJSON.put("style", ppcShowStyle);
                    AppResultBuilder.buildSucResult(result, subJSON);
                } else {
                    AppResultBuilder.buildSucResult(result, "");
                }
                return result;
            }


            AppResultBuilder.buildErrorResult(result, baseResponse.getError_code(), baseResponse.getError_msg(), "");
            return result;
        } catch (Throwable e) {
            e.printStackTrace();
            AppResultBuilder.buildProcessErrorResult(result, e.getMessage(), "");
            return result;
        }
    }
}
